<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
      <el-form-item label="标题" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="标题"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="开始时间" prop="startTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.startTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="endTime">
        <el-date-picker clearable size="small" style="width: 200px"
          v-model="queryParams.endTime"
          type="date"
          value-format="yyyy-MM-dd"
          placeholder="选择结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="是否删除" prop="isDelete">
        <el-select v-model="queryParams.isDelete" placeholder="请选择是否删除" clearable size="small">
          <el-option
            v-for="dict in isDeleteOptions"
            :key="dict.dictValue"
            :label="dict.dictLabel"
            :value="dict.dictValue"
          />
        </el-select>
      </el-form-item>
  
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['content:activity:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['content:activity:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['content:activity:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['content:activity:export']"
        >导出</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="activityList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="标题" align="center" prop="title" />
      <el-table-column label="方法名称和参数" align="center" prop="tap" />
      <el-table-column label="活动描述" align="center" prop="detail" />
      <el-table-column label="开始时间" align="center" prop="startTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.startTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="结束时间" align="center" prop="endTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.endTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否删除" align="center" prop="isDelete" :formatter="isDeleteFormat" />
      <el-table-column label="海报图片" align="center" prop="img" />
      <el-table-column label="H5活动地址" align="center" prop="url" />
      <el-table-column label="是否h5页面" align="center" prop="isH5" :formatter="isH5Format" />
      <el-table-column label="点击浏览量" align="center" prop="tapNum" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['content:activity:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['content:activity:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改活动对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="900px" append-to-body>
       <div style="display:inline-block"> <el-form ref="form" :model="form" :rules="rules" label-width="110px">
        <el-form-item label="活动标题" prop="title">
          <el-input
           v-model="form.title" placeholder="活动标题" />
        </el-form-item>
        <el-form-item label="活动描述" prop="detail">
          <el-input
           type="textarea"
           v-model="form.detail" placeholder="请输入活动描述" />
        </el-form-item>
        <el-form-item label="开始时间" prop="startTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.startTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="endTime">
          <el-date-picker clearable size="small" style="width: 200px"
            v-model="form.endTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="是否删除">
          <el-radio-group v-model="form.isDelete">
            <el-radio
              v-for="dict in isDeleteOptions"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>
          <el-form-item label="上传海报" prop="poster">
          <el-upload
            :headers="videoUpload.headers"
            :action="videoUpload.url"
            :file-list="playBill"
            list-type="picture-card"
            :before-upload="limit_1"
            :on-success="handleMainPhotoSuccess"
            :on-preview="handleMainPhotoPreview"
            :on-remove="handlMainPhotoRemove"
            accept=".jpg,.jpeg,.png,,.JPG,.JPEG"
            :limit="1">
            <i class="el-icon-plus"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否h5页面" prop="isH5">
            <el-radio-group v-model="form.isH5" @change="histTypeUpdate">
            <el-radio
              v-for="dict in isH5options"
              :key="dict.dictValue"
              :label="parseInt(dict.dictValue)"
            >{{dict.dictLabel}}</el-radio>
          </el-radio-group>
        </el-form-item>

        <div v-if="form.isH5==1">
          <el-form-item label="H5活动地址" prop="url">
          <el-input v-model="form.url" placeholder="请输入H5活动地址" />
        </el-form-item>
        </div>
        <div v-if="form.isH5==0">
         <el-form-item label="小程序原生跳转页面" prop="tap">
          <el-input  v-model="form.tap" placeholder="小程序原生跳转页面" />
        </el-form-item>
        </div>
        <el-form-item label="点击浏览量" prop="tapNum">
          <el-input v-model="form.tapNum" placeholder="请输入点击浏览量" />
        </el-form-item>
      </el-form>
      </div>
      <div style="display:inline-block">
       <img :src="form.img" style="width:400px;height:600px" />
      </div>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listActivity, getActivity, delActivity, addActivity, updateActivity, exportActivity } from "@/api/content/activity";
import { getToken } from '@/utils/auth'
export default {
  name: "Activity",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 总条数
      total: 0,
      // 活动表格数据
      activityList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 是否删除字典
      isDeleteOptions: [],
      isH5options:[],
      playBill:[],
      image:"",
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: undefined,
        startTime: undefined,
        endTime: undefined,
        isDelete: undefined,
        url: undefined,
        isH5: undefined,
        tapNum: undefined,
      },
         videoUpload:{
          // 设置上传的请求头部
          headers: { Authorization: "Bearer " + getToken() },
          // 上传的地址
          url: process.env.VUE_APP_BASE_API + "/sys/file/upload",
          url2: process.env.VUE_APP_BASE_API,
        },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    
    this.getList();
    this.getDicts("plat_delete_status").then(response => {
      this.isDeleteOptions = response.data;
    });
    this.getDicts("plat_is_h5").then(response => {
      console.log(response)
      this.isH5options = response.data;
    });
  },
  methods: {
    histTypeUpdate: function(histType) {
      console.log("========histType===" + histType);
      if(histType==1){
        this.form.tap = null
      }else{
         this.form.url = null
      }
    },
    /** 查询活动列表 */
    getList() {
      this.loading = true;
      listActivity(this.queryParams).then(response => {
        this.activityList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 是否删除字典翻译
    isDeleteFormat(row, column) {
      return this.selectDictLabel(this.isDeleteOptions, row.isDelete);
    },
       
       isH5Format(row, column) {

         console.log(row.isH5)
         if(row.isH5==1){
           return "是H5页面";
         }else{
             return "小程序页面";
         }
      // return this.selectDictLabel(this.isDeleteOptions, row.isDelete);
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: undefined,
        title: undefined,
        tap: undefined,
        detail: undefined,
        startTime: undefined,
        endTime: undefined,
        isDelete: "0",
        img: undefined,
        createTime: undefined,
        url: undefined,
        isH5: undefined,
        tapNum: undefined
      };
       this.playBill=[]
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加活动";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getActivity(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改活动";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            updateActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          } else {
            addActivity(this.form).then(response => {
              if (response.code === 200) {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              } else {
                this.msgError(response.msg);
              }
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除活动编号为"' + ids + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delActivity(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(function() {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有活动数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportActivity(queryParams);
        }).then(response => {
          this.download(response.msg);
        }).catch(function() {});
    },
         limit_1(file){
        this.playBill.length>1;
        this.msgInfo("不能大于1张")
      },
       //主图相关 
      handleMainPhotoSuccess(res, file){
        this.playBill.push(file)
        console.log(this.playBill)
         this.form.img = this.playBill[0].response.url
      },
      handleMainPhotoPreview(file){

      },
      handlMainPhotoRemove(file, fileList){
         this.form.img = null
        this.playBill = fileList;
      },
  }
};
</script>